﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using EasyCode.Utility;
using BusinessHandler;
/// <summary>
/// Summary description for CheckRoles
/// </summary>
public class CheckRoles
{
    public CheckRoles()
    {

    }
    public static bool CanView()
    {
        string currentPage = GlobalVariable.CurrentPage;
        if (GlobalVariable.isAdmin == true)
        {
            return true;
        }

        List<UsersInModule> roles = GlobalVariable.GetCurrentRoles;
        if (roles == null)
        {
            return false;
        }
        List<Module> moduleList = GlobalVariable.GetModuleList;
        if (moduleList == null)
        {
            return false;
        }
        Module moduleItem = (from o in moduleList where o.PageName.ToLower() == currentPage.ToLower() select o).SingleOrDefault();
        if (moduleItem == null)
        {
            return false;
        }
        UsersInModule role = roles.Find(delegate(UsersInModule u_m) { return u_m.ModuleID == moduleItem.ModuleID; });
        if (role != null && role.IsDeny != true && (role.IsFull == true || role.IsView == true))
        {
            return true;
        }
        return false;
    }
    public static bool CanEdit()
    {
        string currentPage = GlobalVariable.CurrentPage;
        if (GlobalVariable.isAdmin == true)
        {
            return true;
        }

        List<UsersInModule> roles = GlobalVariable.GetCurrentRoles;
        if (roles == null)
        {
            return false;
        }
        List<Module> moduleList = GlobalVariable.GetModuleList;
        if (moduleList == null)
        {
            return false;
        }
        Module moduleItem = (from o in moduleList where o.PageName.ToLower() == currentPage.ToLower() select o).SingleOrDefault();
        if (moduleItem == null)
        {
            return false;
        }
        UsersInModule role = roles.Find(delegate(UsersInModule u_m) { return u_m.ModuleID == moduleItem.ModuleID; });

        if (role != null && role.IsDeny != true && (role.IsFull == true || role.IsEdit == true))
        {
            return true;
        }
        return false;
    }

    public static bool CanInsert()
    {
        string currentPage = GlobalVariable.CurrentPage;
        if (GlobalVariable.isAdmin == true)
        {
            return true;
        }

        List<UsersInModule> roles = GlobalVariable.GetCurrentRoles;
        if (roles == null)
        {
            return false;
        }
        List<Module> moduleList = GlobalVariable.GetModuleList;
        if (moduleList == null)
        {
            return false;
        }
        Module moduleItem = (from o in moduleList where o.PageName.ToLower() == currentPage.ToLower() select o).SingleOrDefault();
        if (moduleItem == null)
        {
            return false;
        }
        UsersInModule role = roles.Find(delegate(UsersInModule u_m) { return u_m.ModuleID == moduleItem.ModuleID; });

        if (role != null && role.IsDeny != true && (role.IsFull == true || role.IsAdd == true))
        {
            return true;
        }
        return false;
    }

    public static bool CanDelete()
    {
        string currentPage = GlobalVariable.CurrentPage;
        if (GlobalVariable.isAdmin == true)
        {
            return true;
        }

        List<UsersInModule> roles = GlobalVariable.GetCurrentRoles;
        if (roles == null)
        {
            return false;
        }
        List<Module> moduleList = GlobalVariable.GetModuleList;
        if (moduleList == null)
        {
            return false;
        }
        Module moduleItem = (from o in moduleList where o.PageName.ToLower() == currentPage.ToLower() select o).SingleOrDefault();
        if (moduleItem == null)
        {
            return false;
        }
        UsersInModule role = roles.Find(delegate(UsersInModule u_m) { return u_m.ModuleID == moduleItem.ModuleID; });

        if (role != null && role.IsDeny != true && (role.IsFull == true || role.IsDelete == true))
        {
            return true;
        }
        return false;
    }
}